package com.ys.ylideas.util;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class ReportJson {

	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
	private JSONObject header;
	private JSONObject params;
	
	private JSONObject body;
	private JSONObject requestData;
	private JSONObject totalJson;
	
	public void init(){
		header = new JSONObject();
		params = new JSONObject();
		
		body = new JSONObject();
		requestData = new JSONObject();
		totalJson = new JSONObject();
				
	}
	
	public JSONObject getJsonData(String accessToken,JSONObject jsonParams){
		init();
				
		params.put("token", CommonData.token);
		params.put("accessToken", accessToken);		
		header.put("header", params);
		
		
		body.put("body", jsonParams);
		totalJson.putAll(header);
		totalJson.putAll(body);
		
		
		
		//System.out.println("totalJson:"+JSONObject.toJSONString(totalJson, SerializerFeature.WriteMapNullValue));
		return totalJson;		
	}
	
	public String getKeyReportJson(String accessToken){
		init();
		// 处理头部
		params.put("token", CommonData.token);
		params.put("accessToken", accessToken);
		header.put("header", params);
		
		String[] performanceData = new String[] { "impression", "click", "cost", "position", "ctr","cpc","cpm","conversion","bridgeConversion"};
		Date date = new Date();
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -1);  /** 减1天 */
		String startTime = dateFormat.format(cal.getTime())+" "+"00:00:00";
		String endTime = dateFormat.format(cal.getTime())+" "+"23:59:59";
		
		JSONObject realTimeRequestType = new JSONObject();
		realTimeRequestType.put("performanceData", performanceData);
		realTimeRequestType.put("levelOfDetails", 11);
		realTimeRequestType.put("reportType", 14);
		realTimeRequestType.put("statRange", 2);
		realTimeRequestType.put("unitOfTime", 8);
		realTimeRequestType.put("startDate", startTime);
		realTimeRequestType.put("endDate", endTime);
		requestData.put("realTimeRequestType", realTimeRequestType);
		body.put("body", requestData);
		totalJson.putAll(header);
		totalJson.putAll(body);
		return totalJson.toString();
	}
	
	public String getSearchwordReportJson(String accessToken){
		init();
		// 处理头部
		params.put("token", CommonData.token);
		params.put("accessToken", accessToken);
		header.put("header", params);
		
		String[] performanceData = new String[] { "impression", "click", "cost", "ctr"};
		Date date = new Date();
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -1);  /** 减1天 */
		String startTime = dateFormat.format(cal.getTime())+" "+"00:00:00";
		String endTime = dateFormat.format(cal.getTime())+" "+"23:59:59";
		
		JSONObject realTimeRequestType = new JSONObject();
		realTimeRequestType.put("performanceData", performanceData);
		realTimeRequestType.put("levelOfDetails", 12);
		realTimeRequestType.put("reportType", 6);
		realTimeRequestType.put("statRange", 2);
		realTimeRequestType.put("unitOfTime", 5);
		realTimeRequestType.put("startDate", startTime);
		realTimeRequestType.put("endDate", endTime);
		requestData.put("realTimeQueryRequestType", realTimeRequestType);
		body.put("body", requestData);
		totalJson.putAll(header);
		totalJson.putAll(body);
		return totalJson.toString();
	}
	
	public String getCreativeReportJson(String accessToken){
		init();
		// 处理头部
		params.put("token", CommonData.token);
		params.put("accessToken", accessToken);
		header.put("header", params);
		
		String[] performanceData = new String[] { "impression", "click", "cost", "position", "ctr","cpc","cpm","conversion","bridgeConversion"};
		Date date = new Date();
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -1);  /** 减1天 */
		String startTime = dateFormat.format(cal.getTime())+" "+"00:00:00";
		String endTime = dateFormat.format(cal.getTime())+" "+"23:59:59";
		
		JSONObject realTimeRequestType = new JSONObject();
		realTimeRequestType.put("performanceData", performanceData);
		realTimeRequestType.put("levelOfDetails", 7);
		realTimeRequestType.put("reportType", 12);
		realTimeRequestType.put("statRange", 7);
		realTimeRequestType.put("unitOfTime", 8);
		realTimeRequestType.put("startDate", startTime);
		realTimeRequestType.put("endDate", endTime);
		requestData.put("realTimeRequestType", realTimeRequestType);
		body.put("body", requestData);
		totalJson.putAll(header);
		totalJson.putAll(body);
		return totalJson.toString();
	}
}
